#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
const int inf=1e9;
struct Edge{
	int to,w;
};
vector<Edge> g[N];
int n,m,lv[N],val[N],p[N],cl,cr,cnt;
int sum=0,lsum=0,rsum=0;
inline void cmin(int &x,int val){
	x=min(x,val);
}
inline void cmax(int &x,int val){
	x=max(x,val);
}
stack<int> stk;
void dfs(int u){
	if(lv[u]==0){
		//x+val[u]
		cmax(cl,-val[u]),cmin(cr,p[u]-val[u]);
		cnt++,lsum+=val[u],rsum+=val[u];
	}
	else{
		//-x+val[u]<=0
		cmax(cl,val[u]-p[u]),cmin(cr,val[u]);
		cnt--,lsum+=val[u],rsum+=val[u];
	}
	for(auto e:g[u]){
		int v=e.to,w=e.w;
		if(lv[v]==-1){
			lv[v]=lv[u]^1,val[v]=w-val[u];
			stk.push(v);
		}
		else{
			if(lv[v]==lv[u]){
				if(lv[u]==0){
					//val[u]+val[v]+2*x
					int pos=(w-val[u]-val[v])/2;
					if(val[u]+val[v]+2*pos==w) cl=pos,cr=pos;
					else cl=1,cr=0;
				}
				else{
					//val[u]+val[v]-2*x
					int pos=(val[u]+val[v]-w)/2;
					if(val[u]+val[v]-2*pos==w) cl=pos,cr=pos;
					else cl=1,cr=0;
				}
			}
			else{
				if(val[u]+val[v]==w) continue;
				else cl=1,cr=0;
			}
		}
	}
}
signed main(){
	freopen("add.in","r",stdin);
	freopen("add.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>p[i];
	for(int i=1;i<=n;i++) sum+=p[i];
	for(int i=1;i<=m;i++){
		int u,v,w;cin>>u>>v>>w;
		g[u].push_back((Edge){v,w});
		g[v].push_back((Edge){u,w});
	}
	memset(lv,-1,sizeof lv);
	for(int i=1;i<=n;i++){
		if(lv[i]==-1){
			lv[i]=0,cl=0,cr=inf,cnt=0;
			stk.push(i);
			while(!stk.empty()){
				int u=stk.top();stk.pop();
				dfs(u);
			}
			if(cl>cr){
				cout<<"NIE\n";
				return 0;
			}
			else{
				lsum+=cnt*cl,rsum+=cnt*cr;
			}
		}
	}
	cout<<sum-rsum<<" "<<sum-lsum<<"\n";
}
//down finished.

